﻿using System;
using System.Text.RegularExpressions;

namespace CoreLibrary
{
    public class ValidateUtil
    {
        /// <summary>
        /// 是否时间类型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDateTime(string str)
        {
            try
            {
                Convert.ToDateTime(str);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 检测obj是否为空
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool IsNull(object obj)
        {
            if (obj == null)
                return true;
            return false;
        }

        /// <summary>
        /// 是否数字字符串
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumber(string inputData)
        {
            Regex regNumber = new Regex("^[0-9]+$");
            Match m = regNumber.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否数字字符串 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumberSign(string inputData)
        {
            Regex regNumberSign = new Regex("^[+-]?[0-9]+$");
            Match m = regNumberSign.Match(inputData);
            return m.Success;
        }
        
        /// <summary>
        /// 是否是浮点数
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimal(string inputData)
        {
            Regex regDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
            Match m = regDecimal.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否是浮点数 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimalSign(string inputData)
        {
            Regex regDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$");
            Match m = regDecimalSign.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 检测是否有中文字符
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool HasCn(string inputData)
        {
            Regex regCn = new Regex("[\u4e00-\u9fa5]");
            Match m = regCn.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否邮件地址
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsEmail(string inputData)
        {
            Regex regEmail = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            Match m = regEmail.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 判断字符串中是否包含大写字母
        /// </summary>
        /// <param name="loginPwd">要判断的字符串</param>
        public static bool IsContainUpperCase(string loginPwd)
        {
            try
            {
                if (string.IsNullOrEmpty(loginPwd))
                {
                    return false;
                }
                string zipRegex = @"^.*[A-Z]+.*$";
                if (Regex.IsMatch(loginPwd, zipRegex))
                {
                    return true;
                }
                return false;
            }
            catch (FormatException)
            {
                return false;
            }
        }

        /// <summary>
        /// 判断字符串中是否包含小写字母
        /// </summary>
        /// <param name="loginPwd">要判断的字符串</param>
        public static bool IsContainLowerCase(string loginPwd)
        {
            try
            {
                if (string.IsNullOrEmpty(loginPwd))
                {
                    return false;
                }
                string regex = @"^.*[a-z]+.*$";
                if (Regex.IsMatch(loginPwd, regex))
                {
                    return true;
                }
                return false;
            }
            catch (FormatException)
            {
                return false;
            }
        }

        /// <summary>
        /// 判断字符串中是否包含字母(不区分大小写)
        /// </summary>
        /// <param name="loginPwd">要判断的字符串</param>
        public static bool IsContainCase(string loginPwd)
        {
            try
            {
                if (string.IsNullOrEmpty(loginPwd))
                {
                    return false;
                }
                string regex = @"^.*[A-Za-z]+.*$";
                if (Regex.IsMatch(loginPwd, regex))
                {
                    return true;
                }
                return false;
            }
            catch (FormatException)
            {
                return false;
            }
        }

        /// <summary>
        /// 判断字符串中是否包含数字
        /// </summary>
        /// <param name="loginPwd">要判断的字符串</param>
        public static bool IsContainNumber(string loginPwd)
        {
            try
            {
                if (string.IsNullOrEmpty(loginPwd))
                {
                    return false;
                }
                string regex = @"^.*[0-9]+.*$";
                if (Regex.IsMatch(loginPwd, regex))
                {
                    return true;
                }
                return false;
            }
            catch (FormatException)
            {
                return false;
            }
        }

        /// <summary>
        /// 判断字符串中是否包含特殊字符
        /// </summary>
        /// <param name="loginPwd">要判断的字符串</param>
        public static bool IsContainSymbol(string loginPwd)
        {
            try
            {
                if (string.IsNullOrEmpty(loginPwd))
                {
                    return false;
                }
                string regex = @"^.*[^A-Za-z0-9]+.*$";
                if (Regex.IsMatch(loginPwd, regex))
                {
                    return true;
                }
                return false;
            }
            catch (FormatException)
            {
                return false;
            }
        }
    }
}
